/**
 * $version:  0.1 
 * $Date: 2012-05-10 
 *
 * Copyright (C) 2010-2012 Jawa Software. All rights reserved.
 *
 */
package org.jawa.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;
import org.jawa.core.exception.NotFoundException;
import org.jawa.menu.Menu;
import org.jawa.menu.MenuManager;

/**
 * 左侧菜单
 * @author Y-N
 *
 */
public class SidebarTag extends BodyTagSupport {

	private String css;
	private String currentcss;
	private String headercss;
	private SubSidebarTag subsidebarTag;

	@Override
	public int doStartTag() throws JspException {
		return EVAL_BODY_BUFFERED;
	}

	@Override
	public int doEndTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

		if (getBodyContent().getString() == null) {
			throw new JspException("错误, sidebar 标签没有内容模板.");
		}

		if (subsidebarTag.getBody() == null) {
			throw new JspException("错误, subsidebarTag 标签没有内容模板.");
		}

		Long pageID = (Long) request.getAttribute("pageID");

		//当前菜单页
		Menu currentMenu = null;
		//当前菜单上级菜单页
		Menu parrentMenu = null;
		try {
			currentMenu = MenuManager.getInstance().getMenu(pageID);
			parrentMenu = MenuManager.getInstance().getMenu(currentMenu.getMenuParentId());
		} catch (NotFoundException e) {
			//忽略
		}

		if (null == currentMenu || null == parrentMenu) {
			return EVAL_PAGE;
		}

		if (pageID != null) {

			JspWriter out = pageContext.getOut();

			List<Menu> sideMenus = MenuManager.getInstance().getChildrenMenus(parrentMenu.getMenuParentId());

			if (sideMenus.size() > 0) {

				StringBuilder buf = new StringBuilder();

				buf.append("<ul ");

				if (null != getHeadercss()) {
					buf.append(" class=\"").append(getHeadercss() + "\" ");
				}

				buf.append(">");

				String body = getBodyContent().getString();

				for (int i = 0; i < sideMenus.size(); i++) {
					Menu siderMenu = (Menu) sideMenus.get(i);
					String value = body;
					if (value != null) {
						value = StringUtils.replace(value, "[id]", Long.toString(siderMenu.getMenuId()));

						if (siderMenu.getMenuUrl() != null && !siderMenu.getMenuUrl().trim().equals("#")) {
							value = StringUtils.replace(value, "[url]", request.getContextPath() + "/"
									+ siderMenu.getMenuUrl());
						} else {
							value = StringUtils.replace(value, "[url]", "#");
						}

						value = StringUtils.replace(value, "[name]", siderMenu.getMenuName());

						value = StringUtils.replace(value, "[description]", siderMenu.getRemark());
					}

					buf.append("<li");

					String css = getCss();


					if (null != parrentMenu && parrentMenu.getMenuId().equals(pageID)) {
						css = getCurrentcss();
					}

					if (null != css) {
						buf.append(" class=\"").append(css).append("\" ");
					}

					buf.append(">" + value + "</li>");

					
					//循环下级菜单
					List<Menu> subSideMenus = MenuManager.getInstance().getChildrenMenus(siderMenu.getMenuId());
					for (int j = 0; j < subSideMenus.size(); j++) {
						Menu subSiderMenu = (Menu) subSideMenus.get(j);
						String svalue = getSubsidebarTag().getBody();
						if (svalue != null) {
							svalue = StringUtils.replace(svalue, "[id]", Long.toString(subSiderMenu.getMenuId()));

							if (subSiderMenu.getMenuUrl() != null && !subSiderMenu.getMenuUrl().trim().equals("#")) {
								svalue = StringUtils.replace(svalue, "[url]", request.getContextPath() + "/"
										+ subSiderMenu.getMenuUrl());
							} else {
								svalue = StringUtils.replace(svalue, "[url]", "#");
							}

							svalue = StringUtils.replace(svalue, "[name]", subSiderMenu.getMenuName());

							svalue = StringUtils.replace(svalue, "[description]", subSiderMenu.getRemark());
						}

						buf.append("<li");

						css = subsidebarTag.getCss();

						if (subSiderMenu.getMenuId().equals(pageID)) {
							css = subsidebarTag.getCurrentcss();
						}

						if (null != css) {
							buf.append(" class=\"").append(css).append("\" ");
						}

						buf.append(">" + svalue + "</li>");

					}

				}

				buf.append("</ul>");

				try {
					out.write(buf.toString());
				} catch (IOException ioe) {
					throw new JspException(ioe.getMessage());
				}

			}

		}
		return EVAL_PAGE;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

	public String getCurrentcss() {
		return currentcss;
	}

	public void setCurrentcss(String currentcss) {
		this.currentcss = currentcss;
	}

	public String getHeadercss() {
		return headercss;
	}

	public void setHeadercss(String headercss) {
		this.headercss = headercss;
	}

	public SubSidebarTag getSubsidebarTag() {
		return subsidebarTag;
	}

	public void setSubSidebar(SubSidebarTag subsidebarTag) {
		this.subsidebarTag = subsidebarTag;
	}

}